package com.youpingou.controller;

import com.youpingou.domain.Address;
import com.youpingou.service.AddressService;
import com.youpingou.vo.BaseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping
public class AddressController {

    @Autowired
    private HttpServletRequest request;
    // service
    @Autowired
    private AddressService addressService;

    /**
     * 这个不能放行
     * @return
     */
    @GetMapping("/address")
    public BaseResult findAddressByUserId(){
        //1. 调用service
        List<Address> list = addressService.findAddressByUserId(request);

        return BaseResult.ok(list);
    }


    @GetMapping("addressById")
    public Address findAddressByAddressId(@RequestParam("addressId")Integer addressId){
        Address address = addressService.findAddressByAddressId(addressId);
        return address;
    }

    @PostMapping("/address")
    public BaseResult addAddress(@RequestBody Address address){
        //1. 调用service
        Integer id = addressService.addAddress(address,request);

        return BaseResult.ok(id);
    }

    @PutMapping("/address")
    public BaseResult updateAddress(@RequestBody Address address){
        //1. 调用service
        Integer id = addressService.updateAddress(address);

        return BaseResult.ok();
    }
}